package com.apes.autoparts.parts.model;

import com.apes.framework.jpa.BaseModel;
import com.apes.framework.jpa.DefaultPersistListener;
import com.apes.framework.jpa.Name;
import com.apes.framework.jpa.comment.Comment;
import com.apes.framework.jpa.generator.ApesGenerator;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.SelectBeforeUpdate;

import javax.persistence.*;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Getter
@Setter
@SelectBeforeUpdate
@DynamicInsert
@DynamicUpdate
@Entity
@EntityListeners(DefaultPersistListener.class)
@Table(name = AutoModel.CONST_TABLE_NAME)
public class AutoModel extends BaseModel {
    public static final String CONST_TABLE_NAME = "auto_model";


    @Id
    @Column(name = "id")
    @ApesGenerator(code = CONST_TABLE_NAME, name = "车型")
    @Comment("唯一码")
    private String id;

    @Name
    @Column(name = "selector_id", length = 32)
    @Comment("车型编码")
    private String selectorId;

    @Column(name = "code")
    @Comment("车型代码")
    private String code;

    @Column(name = "name")
    @Comment("车型名称")
    private String name;

    @Name
    @Column(name = "brand_code")
    @Comment("品牌code")
    private String brandCode;

    @Column(name = "brand_name")
    @Comment("品牌code")
    private String brandName;

    @Column(name = "QCCPMC")
    @Comment("汽车厂牌名称")
    private String QCCPMC;

    @Column(name = "PZCXID")
    @Comment("配置车型编码")
    private String PZCXID;

    @Column(name = "image_url")
    @Comment("车型图片")
    private String imageUrl;


    @Column(name = "timestamp")
    @Comment("时间戳")
    private Date timestamp;

    /***
     * 属性详情
     */
    @OneToMany(mappedBy = "selectorId", cascade = CascadeType.ALL, orphanRemoval = true)
    private List<ModelProp> props = new ArrayList<>();

    public List<ModelProp> getProps() {
        for (ModelProp item : props) {
            item.setSelectorId(this);
        }
        return props;
    }

    /***
     * 特性值
     */
    @OneToMany(mappedBy = "selectorId", cascade = CascadeType.ALL, orphanRemoval = true)
    private List<ModelFeature> features = new ArrayList<>();

    public List<ModelFeature> getFeatures() {
        for (ModelFeature item : features) {
            item.setSelectorId(this);
        }
        return features;
    }

    /***
     * 配置
     */
    @Name
    @OneToMany(mappedBy = "selectorId", cascade = CascadeType.ALL, orphanRemoval = true)
    private List<ModelConfig> modelConfigs = new ArrayList<>();

    public List<ModelConfig> getModelConfigs() {
        for (ModelConfig item : modelConfigs) {
            item.setSelectorId(this);
        }
        return modelConfigs;
    }


}
